<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form @submit.native.prevent>
        <el-date-picker
          v-model="listQuery.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <el-input placeholder="订单号" style="width: 200px" class="filter-item" v-model="listQuery.order_id" />

        <el-select style="width: 200px" v-model="listQuery.status" placeholder="分润状态">
          <el-option
            v-for="(item, index) in [
              { id: -1, value: '全部' },
              { id: 0, value: '待结算' },
              { id: 2, value: '已结算' },
            ]"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-select style="width: 200px" v-model="listQuery.agent_id" placeholder="所属代理商">
          <el-option
            v-for="(item, index) in [{ id: -1, name: '全部' }].concat(allAgent || [])"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          native-type="submit"
          @click="fetchData(1)"
          >查询</el-button
        >
      </el-form>
    </div>
    <!-- 表格内容 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="加载中..." border fit highlight-current-row>
      >
      <el-table-column label="订单号" align="center">
        <template slot-scope="scope">{{ scope.row.order_id }}</template>
      </el-table-column>
      <el-table-column label="代理商" align="center">
        <template slot-scope="scope">{{ scope.row.agent.name }}</template>
      </el-table-column>
      <el-table-column label="卡号码" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="$router.push(`/card/info/${scope.row.card.id}`)">{{
            scope.row.card.business_code
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="卡套餐" align="center">
        <template slot-scope="scope">{{ scope.row.meal.name }}</template>
      </el-table-column>
      <el-table-column label="分润金额" align="center">
        <template slot-scope="scope">{{ scope.row.fee }}</template>
      </el-table-column>
      <el-table-column label="分润状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0 && scope.row.fee >= 0">待结算</el-tag>
          <el-tag v-if="scope.row.status === 0 && scope.row.fee < 0" type="danger">待结算，分润被撤销</el-tag>
          <el-tag v-if="scope.row.status === 2" type="success">已结算</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import waves from '@/directive/waves'; // waves directive
import Pagination from '@/components/Pagination'; // secondary package based on el-pagination
import { getList } from '@/api/share';
import dayjs from 'dayjs';
import { mapGetters } from 'vuex';

export default {
  directives: {
    waves,
  },
  components: {
    Pagination,
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1, // 当前页码
        size: 50, // 每页大小

        sortProp: '',
        sortOrder: '',

        settle_id: this.$route.query.settle_id,
        status: this.$route.query.status !== undefined ? Number(this.$route.query.status) : -1,
        agent_id: Number(this.$route.query.agent_id) || -1,
        time: this.$route.query.time
          ? [
              new Date(Number(this.$route.query.time.split(',')[0])),
              new Date(Number(this.$route.query.time.split(',')[1])),
            ]
          : null,
      },
    };
  },
  computed: {
    ...mapGetters(['allAgent']),
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      const res = await getList({
        ...this.listQuery,
        timeStart: this.listQuery.time && dayjs(this.listQuery.time[0]).format('YYYY-MM-DD HH:mm:ss'),
        timeEnd: this.listQuery.time && dayjs(this.listQuery.time[1]).format('YYYY-MM-DD HH:mm:ss'),
      });
      this.total = res.data.total;
      this.list = res.data.list;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
  },
};
</script>
<style scoped lang="scss">
.btn {
  // margin: 5px;
}

.filter-container {
  .filter-item {
    margin-bottom: 10px;
  }
}
</style>
